package t_841

var openRoomsMap map[int][]int
var keys []bool

func canVisitAllRooms(rooms [][]int) bool {
	openRoomsMap = make(map[int][]int, 0)
	keys = make([]bool, len(rooms))
	// 先去0号房 获取其他房间钥匙
	getKeys(rooms, 0)
	// 是否能够进入所有房间
	return checkIfEnterAllRooms(keys)
}

func checkIfEnterAllRooms(keys []bool) bool {
	for i := 0; i < len(keys); i++ {
		if keys[i] == false {
			return false
		}
	}
	return true
}

func getKeys(rooms [][]int, i int) {
	openRoomsMap[i] = rooms[i]
	keys[i] = true
	for v := 0; v < len(openRoomsMap[i]); v++ {
		// 没有去过的房间 才去
		if keys[openRoomsMap[i][v]] == false {
			getKeys(rooms, openRoomsMap[i][v])
		}
	}
}
